# -*- coding: utf-8 -*-
'''
Created on 09/nov/2009

@author: sax
'''

from copy import copy
from django import forms, forms, template
from xadmin import helpers, widgets, widgets
from xadmin.util import unquote, flatten_fieldsets, get_deleted_objects, model_ngettext, model_format_dict
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied, PermissionDenied
from django.core.urlresolvers import reverse, resolve, NoReverseMatch
from django.db import models, models, transaction
from django.db.models.fields import BLANK_CHOICE_DASH
from django.forms.formsets import all_valid
from django.forms.models import BaseInlineFormSet, modelform_factory, modelformset_factory, inlineformset_factory
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template.context import Context
from django.template.loader import get_template
from django.utils.datastructures import SortedDict
from django.utils.encoding import force_unicode, force_unicode
from django.utils.functional import curry, update_wrapper, update_wrapper
from django.utils.html import escape
from django.utils.safestring import mark_safe, mark_safe
from django.utils.text import capfirst, get_text_list
from django.utils.translation import ugettext as _, ugettext as _, ungettext, ugettext_lazy
from xadmin.ajax import DatePickerWidget
from xadmin.ajax.service import AjaxService
from xadmin.ajax.widgets.autocomplete import AjaxLookupInput
from xadmin.util import unquote
from xadmin.widgets import *
from xadmin.xbuttons import XModelAdminButton, XActionButton
import logging
LOGGER = logging.getLogger('xadmin.xamodeladmin')




class HistoryStrategy(object):
    def history_view(self, request, object_id, extra_context=None):
        "The 'history' admin view for this model."
        from django.contrib.admin.models import LogEntry
        model = self.model
        opts = model._meta
        app_label = opts.app_label
        action_list = LogEntry.objects.filter(
            object_id = object_id,
            content_type__id__exact = ContentType.objects.get_for_model(model).id
        ).select_related().order_by('action_time')
        # If no history was found, see whether this object even exists.
        obj = get_object_or_404(model, pk=object_id)
        context = {
            'title': _('Change history: %s') % force_unicode(obj),
            'action_list': action_list,
            'module_name': capfirst(force_unicode(opts.verbose_name_plural)),
            'object': obj,
            'root_path': self.admin_site.root_path,
            'app_label': app_label,
        }
        context.update(extra_context or {})
        context_instance = template.RequestContext(request, current_app=self.admin_site.name)
        return render_to_response(self.object_history_template or [
            "admin/%s/%s/object_history.html" % (app_label, opts.object_name.lower()),
            "admin/%s/object_history.html" % app_label,
            "admin/object_history.html"
        ], context, context_instance=context_instance)
    
    
